#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Time          : 2024/5/28 9:35
# File          : many_db.py
# @Author       : MingTai
# @Department   : automation_qa
from sqlalchemy import Column, Integer, String, DateTime

from app.business.mysql_init.init_sql_alchemy import db
from app.business.sql_demo.flask_sql_alchemy import app
from app.utils.mysql_util.mysqldb import MysqlSession

"""
数据库连接池测试，次选
"""


class TableName2(db.Model):
    __tablename__ = 'task_conclusions'
    id = db.Column(db.Integer(), primary_key=True, comment='id')
    task_id = db.Column(db.String(128), nullable=False, comment='task_id')
    conclusion = db.Column(db.String(255), comment='结论')
    citation = db.Column(db.String(255), comment='注释')

    @classmethod
    def get_task_id(cls):
        try:
            with MysqlSession() as db_session:
                task_id = db_session.query(cls.task_id).with_for_update().all()
                res_data = []
                for case in task_id:
                    res_data.append(case[0])
                print(type(res_data), res_data)
                return res_data
        except Exception as e:
            app.logger.error("数据库错误，get_badcase_report_data：{}".format(e))
            return []


class TableName(db.Model):
    __tablename__ = 'dm_report_info'

    id = Column(Integer(), primary_key=True, comment='id')
    report_id = Column(String(255), nullable=False, comment='评测报告名称')
    data_set_lpai_path = Column(String(255), nullable=False, comment='数据集路径')
    release_date = Column(String(100), nullable=False, comment='发版日期')
    model_version = Column(String(100), nullable=False, comment='模型版本')
    lpai_url = Column(String(255), nullable=False, comment='lpai链接')
    report_msg = Column(String, nullable=False, comment='评测备注')
    create_time = Column(DateTime, server_default=db.text('CURRENT_TIMESTAMP'), comment='创建时间')
    update_time = Column(DateTime, server_default=db.text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
                         comment='更新时间')

    @classmethod
    def update_report_data(cls, report_id, **kwargs):
        try:
            with MysqlSession() as db_session:
                db_session.query(cls).filter(cls.report_id == report_id).update(kwargs)
        except Exception as e:
            app.logger.error("数据库错误，update_report_data：{}".format(e))
            return False

    @classmethod
    def get_report_msg(cls, report_id):
        try:
            with MysqlSession() as db_session:
                report_msg = db_session.query(cls.report_msg).filter(cls.report_id == report_id).first()
                return report_msg[0]
        except Exception as e:
            app.logger.error("数据库错误，update_report_data：{}".format(e))
            return ''

    @classmethod
    def get_model_versions(cls):
        try:
            with MysqlSession() as db_session:
                dm_datas = db_session.query(cls).order_by(cls.create_time.desc()).all()
                version_set = {}
                for dm in dm_datas:
                    version_set[dm.model_version] = {'data_set_lpai_path': dm.data_set_lpai_path,
                                                     'release_date': dm.release_date,
                                                     'model_version': dm.model_version}
                return version_set
        except Exception as e:
            app.logger.error("数据库错误，get_model_versions：{}".format(e))
